<?php

namespace app\api\controller\fruit\tt;

use app\api\controller\Base;
use think\Request;

class BaseWarrior extends Base
{

    // 无需登录的方法
    protected $noNeedLogin = [];

    // 用户实例
    protected $user;

    // 审核控制版本号
    protected $control_version = '0.0.0';

    // 当前版本号
    protected $version;

    // 每隔几关赠送无尽模式入场券
    protected $ticket_level = 20;

    protected $important_list = array(
        array(
            'name' => '游趣娱乐',
            'appid' => 'wxa9002f27f3ed1519',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/game/icon/you.jpg'
        ),
        array(
            'name' => '萌消团',
            'appid' => 'wxd26399f7e0a403d0',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mxt.png'
        ),
        array(
            'name' => '一起来合体',
            'appid' => 'wxd382be58d8c6d6c9',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/heti.png'
        ),
        array(
            'name' => '一起飞飞飞',
            'appid' => 'wxe42ac5dd0f116b26',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/fly.jpg'
        ),
        array(
            'name' => '拯救查尔斯',
            'appid' => 'wxc76853fdcf188c3e',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/charles.jpg'
        )
    );

    // 构造方法
    public function __construct(Request $request = null)
    {
        header('Access-Control-Allow-Origin:*'); // 跨域
        parent::__construct($request);
        $this->version = $request->param('version') ?: '0.0.0';
    }

    // 检测当前控制器和方法是否匹配传递的数组
    public function match($arr = [], $actionname)
    {
        $arr = is_array($arr) ? $arr : explode(',', $arr);
        if (!$arr) {
            return FALSE;
        }
        $arr = array_map('strtolower', $arr);
        // 是否存在
        if (in_array(strtolower($actionname), $arr) || in_array('*', $arr)) {
            return TRUE;
        }

        // 没找到匹配
        return FALSE;
    }

    // 获取指定数量的不重复随机数
    public function unique_rand($min, $max, $num)
    {
        $count = 0;
        $return_arr = array();
        while ($count < $num) {
            $return_arr[] = mt_rand($min, $max);
            $return_arr = array_flip(array_flip($return_arr));
            $count = count($return_arr);
        }
        shuffle($return_arr);
        return $return_arr;
    }

    // 向微信服务器获取openid和session
    protected function jscode2session($code, $anonymous_code)
    {
        $url = 'https://developer.toutiao.com/api/apps/jscode2session';
        $params['appid'] = 'tt0e7a78c79150b87a';
        $params['secret'] = '4a051a2a7c7a599c606da3c61d90d2193e1ba294';
        $params['code'] = $code;
        $params['anonymous_code'] = $anonymous_code;

        // 请求微信后台获取openid
        $res = $this->http($url, $params);
        $data = json_decode($res, true);
        if (array_key_exists('openid', $data)) {
            return $data;
        } else {
            return false;
        }
    }

}
